package com.imooc.security.server.auth;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
 * @ClassName OAuth2LogoutSuccessHandler
 * @Description TODO 自定义退出成功处理器，重定向到 redirect_uri
 * @Author wushaopei
 * @Date 2021/5/16 0:19
 * @Version 1.0
 */
@Component
public class OAuth2LogoutSuccessHandler implements LogoutSuccessHandler {

    /* (non-Javadoc)
     * @see org.springframework.security.web.authentication.logout.LogoutSuccessHandler#onLogoutSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        // 从request获取到redirect_uri，进行重定向
        String redirectUri = request.getParameter("redirect_uri");
        if(!StringUtils.isEmpty(redirectUri)) {
            response.sendRedirect(redirectUri);
        }
    }

}
